define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/accredit/index' + location.search,
                    add_url: 'user/accredit/add',
                    // edit_url: 'user/accredit/edit',
                    del_url: 'user/info/del',
                    multi_url: 'user/accredit/multi',
                    import_url: 'user/accredit/import',
                    table: 'user_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'first_cate', title: __('一级分类'), searchList: Config.categoryList, formatter: Table.api.formatter.flag },
                        { field: 'two_cate', title: __('二级分类'), searchList: Config.categoryList, formatter: Table.api.formatter.flag },
                        { field: 'user.username', title: __('名称'), operate: 'LIKE' },
                        { field: 'user.mobile', title: __('手机号'), operate: 'LIKE' },
                        { field: 'group.name', title: __('身份'), operate: 'LIKE' },
                        { field: 'accredit_scale', title: __('授权比例（%）'), operate: 'LIKE' },
                        { field: 'address', title: __('区域'), operate: 'LIKE' },
                        { field: 'accredit', title: __('是否授权佣金'), searchList: { "0": __('未授权'), "1": __('已授权') }, formatter: Table.api.formatter.normal },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'accredit',
                                    title: '授权',
                                    text: '授权',
                                    extend: 'data-area=["90%","90%"]',
                                    refresh:true,
                                    confirm:function (row) {
                                        if(row.accredit==1){
                                            return '是否取消授权？'
                                        }else{
                                            return '是否确认授权？'
                                        }
                                    },
                                    classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                    url: 'user/accredit/accredit',//接口地址 控制器名/方法名

                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        accredit: function () {
            Controller.api.bindevent();
        },
        add: function () {

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
